package com.xiaofan.controller;

import com.alibaba.druid.sql.visitor.functions.If;
import com.xiaofan.bean.BaseRespVo;
import com.xiaofan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.xiaofan.bean.User;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author: 晓帆
 * @description:
 * @create: 2021-06-29 22:29
 **/
@RestController
@RequestMapping( "user")
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping(value = "quick",method = RequestMethod.GET)
    public ModelAndView save(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = {"aaa","sas"})
    public String add(HttpServletRequest httpServletRequest, Model model){
        //获取用户名
        String name = httpServletRequest.getParameter("username");
        //通过用户名获取用户的余额
        Integer money = userService.selectMoney(name);
        if (money==null){
            //设置参数并完成跳转
            model.addAttribute("name","该账户不存在:");
            model.addAttribute("money","null");
            return "success";
        }
        System.out.println(money);
        model.addAttribute("name",name);
        model.addAttribute("money",money);
        return "success";
    }
    @RequestMapping("register")
    public BaseRespVo register(User user){
        if ((user.getUsername()==null||user.getUsername().trim().length()==0)||(user.getPassword()==null||user.getPassword().trim().length()==0)){
            return BaseRespVo.fail();
        }
        Integer registerState = userService.registerByUser(user);
        if (registerState==1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }
    @RequestMapping("update")
    public BaseRespVo update(User user){
        if (user.getPassword()==null||user.getPassword().trim().length()==0){
            return BaseRespVo.fail();
        }
        Integer State = userService.updateByUser(user);
        if (State!=0){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();

    }
    @RequestMapping("date")
    public BaseRespVo typeConversion(@DateTimeFormat(pattern = "yy-MM-dd") Date date){
        System.out.println(date);
        return BaseRespVo.ok();
    }
}
